/*
 * =====================================================================================
 *
 *       Filename:  sqstack.c
 *
 *    Description:  
 *
 *        Version:  1.0
 *        Created:  10/31/2010 11:05:49 AM
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  YOUR NAME (), 
 *        Company:  
 *
 * =====================================================================================
 */
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include "sqstack.h"


status init_stack(sqstack *S)
{
   (S)->base = (sqdata *)malloc(STACK_INIT_SIZE * sizeof(sqdata));
   //if(!s->base)
   //    return -1;
   (S)->top = (S)->base;
   (S)->stacksize = STACK_INIT_SIZE ;

   return 0;
   
}

status destroy_stack(sqstack *S)
{
    free(S->base);
    S->base = NULL;
    S->top = NULL;
    return 0;
}

status clear_stack(sqstack *S)
{
    sqstack *p = S;
    p->top = p->base;
    return 0;
}

status stack_is_empty(sqstack *S)
{
    if( S->base == S->top)
        return 1;
    else
        return 0;
}

int stack_length(sqstack *S)
{
    return S->top - S->base;
}

status get_top(sqstack *S, sqdata *e)
{
    if(!stack_is_empty(S))
    {
        *e = *--S->top;
        return 0;
    }else
    {
        return -2;
    }
    
}
status push(sqstack *S, sqdata e)
{
    if(S->top - S->base == S->stacksize)
    {
        S->base = (sqdata *)realloc(S->base, (S->stacksize + STACKINCREMENT)*sizeof(sqdata));
        
    }
    if(!S->base)
        return -1;
    *S->top++ = e;
    
    return 0;
}
status pop(sqstack *S, sqdata *e)
{
    if(stack_is_empty(S))
    {
        return -2;
    }
    else
    {
        *e = *--S->top;
        return 0;
    }
}
status stack_traverse(sqstack *S, status(*visit)())
{
     
}

status display(sqstack *S)
{
    if(stack_is_empty(S))
        {
            printf("empty");
            return 0;
        }
    int i;
    for(i=0;i<stack_length(S);i++)
        printf("%d ",S->base[i]);

    return 0;
}
